﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using PhotoUploader.Services;
using System.Windows.Media.Imaging;
using PhotoUploader.Models;

namespace YiDingCanWp7
{
    public partial class AddShopPicture : PhoneApplicationPage
    {
        string shopid = string.Empty;
        public AddShopPicture()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            shopid = NavigationContext.QueryString["shopid"];
        }
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            Picture picture = PictureRepository.Instance.GetPicture("WideShot.jpg");
            UploadService.ServiceUploadLocationURL = BaseConfig.BaseUrl+"/Shop/UserAddShopPicture?GetDeviceUniqueId=" + PhoneUtils.GetDeviceUniqueId()+"&shopid="+shopid; 
            if (picture != null)
            {
                progresswideshot.DataContext = picture;
                UploadService.Instance.StartUpload(picture);
            }
            else
            {
                MessageBox.Show("请先选择新的照片上传");
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            //CameraCaptureTask cct = new CameraCaptureTask();
            // cct.Completed += new EventHandler<PhotoResult>(cct_Completed);
            // cct.Show();
            PhotoChooserTask pc = new PhotoChooserTask();
            //是否裁切相片，並設定裁切後相片的最大高度以及寬度
            //pc.PixelHeight = 30;
            //pc.PixelWidth = 80;
            //設定是否出現拍照的按鈕(位於Application Bar)
            pc.ShowCamera = true;
            pc.Completed += new EventHandler<PhotoResult>(pc_Completed);
            pc.Show();
        }
        void pc_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                var capturedPicture = new Picture("WideShot.jpg", e.ChosenPhoto) { NickName = "整体损坏照片" };
                PictureRepository.Instance.SavePicture(capturedPicture);
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(e.ChosenPhoto);
                imagewide.Source = bitmap;
            }
        }

        private void imagewide_KeyUp(object sender, KeyEventArgs e)
        {

        }
    }
}